﻿using System;
using System.Collections.Generic;

namespace Soulver.Model
{
    public static class QuestionParser
    {
        private static List<string> answers;

        public static string[] Parse(string[] lines)
        {
            ExpressionCompiler.Reset();
            answers = new List<string>();

            foreach (string s in lines)
            {
                if (ValueVariable.IsMatch(s))
                {
                    var v = ValueVariable.Parse(s);
                    ExpressionCompiler.AddVariable(v.Key, v.Value);
                    answers.Add(v.Value.ToString());
                }
                else if (LineVariable.IsMatch(s))
                {
                    var v = LineVariable.Parse(s);
                    answers.Add(v);
                }
                else if (ExpressionCompiler.IsMatch(s))
                {
                    answers.Add(ExpressionCompiler.Parse(s));
                }
                else
                {
                    answers.Add(String.Empty);
                }
            }

            return answers.ToArray();
        }

        public static string GetAnswerAt(int index)
        {
            if (index >= answers.Count)
                return null;

            return answers[index];
        }
    }
}